package com.example.restatr.Atcivity.LogisticsQuery;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.button.MaterialButton;

import org.json.JSONException;
import org.json.JSONObject;

public class CreateComplaintAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private EditText Distance;
    private EditText gasFilling;
    private EditText amount;
    private MaterialButton mileageCommit;
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_complaint_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        Distance = findViewById(R.id.Distance);
        gasFilling = findViewById(R.id.gasFilling);
        amount = findViewById(R.id.amount);
        mileageCommit = findViewById(R.id.mileageCommit);
        Tools.SetBarTitle(toolbar, barTitle, "发起投诉", search, this, false);
        gasFilling.setFocusable(false);
        gasFilling.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] items = {"快递丢失", "快递破损", "快递被拆封", "工作人员态度恶劣", "未送货上门"};
                String[] ids = {"0", "1", "2", "3", "4"};
                new AlertDialog.Builder(CreateComplaintAt.this)
                        .setItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                gasFilling.setText(items[which]);
                                id=ids[which];
                                dialog.dismiss();
                            }
                        }).create().show();

            }
        });
    }

    public void onClick(View view) {
        if (mileageCommit.getText().toString().equals("添加")){
            if (Tools.isEmptyText(Distance) && Tools.isEmptyText(gasFilling) && Tools.isEmptyText(amount)) {
                new Thread(() -> {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("companyId", getIntent().getStringExtra("id"));
                        jsonObject.put("infoNo", Distance.getText().toString());
                        jsonObject.put("questionType",id);
                        jsonObject.put("description", amount.getText().toString());
                        String post = Tools.Post("/prod-api/api/logistics-inquiry/logistics_complaint", MainActivity.sp.getString("token", ""), jsonObject.toString());
                        JSONObject jx = new JSONObject(post);
                        String msg = jx.getString("msg");
                        int code = jx.getInt("code");
                        runOnUiThread(() -> {
                            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
                            if (code == 200) {
                                mileageCommit.setText("查看我的投诉");
                            }
                        });
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }else {
            startActivity(new Intent(CreateComplaintAt.this,MyComplaintsListAt.class));
        }
    }
}